/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQueries( {
    @NamedQuery(name="findAllMyResponsibleTasks",
                query="SELECT DISTINCT t FROM Task t, IN (t.responsibleContactables) p WHERE p = :contactable"),
    @NamedQuery(name="findAllMyInterestedTasks",
                query="SELECT DISTINCT t FROM Task t, IN (t.interestedContactables) p WHERE p = :contactable"),
    @NamedQuery(name="findMyResponsibleTasks",
                query="SELECT DISTINCT t FROM Search s, IN (s.searchLevels) l, IN (l.nodes) k, Task t, IN (t.responsibleContactables) p WHERE s = :search AND t = k AND p = :contactable"),
    @NamedQuery(name="findMyInterestedTasks",
                query="SELECT DISTINCT t FROM Search s, IN (s.searchLevels) l, IN (l.nodes) k, Task t, IN (t.interestedContactables) p WHERE s = :search AND t = k AND p = :contactable")
})
public class Task extends Node {
    private static final long serialVersionUID = 1L;

    private int priority;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date startDate;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date stopDate;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date cachedStartDate;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date cachedStopDate;

    @ManyToOne
    private Contactable creator;

    @ManyToOne
    private Contactable owner;

    @ManyToMany
    @JoinTable(name="TASK_RESPONSIBLE_CONTACTABLE")
    private Collection<Contactable> responsibleContactables = new ArrayList<Contactable>();

    @ManyToMany
    @JoinTable(name="TASK_INTERESTED_CONTACTABLE")
    private Collection<Contactable> interestedContactables = new ArrayList<Contactable>();

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getStopDate() {
        return stopDate;
    }

    public void setStopDate(Date stopDate) {
        this.stopDate = stopDate;
    }

    public Date getCachedStartDate() {
        return cachedStartDate;
    }

    public Date getCachedStopDate() {
        return cachedStopDate;
    }

    public Collection<Contactable> getInterestedContactables() {
        return interestedContactables;
    }

    public Collection<Contactable> getResponsibleContactables() {
        return responsibleContactables;
    }

    public int getPriority() {
        return priority;
    }

    public Contactable getCreator() {
        return creator;
    }

    public Contactable getOwner() {
        return owner;
    }

    public void setCreator(Contactable creator) {
        this.creator = creator;
    }

    public void setInterestedContactables(Collection<Contactable> interestedContactables) {
        this.interestedContactables = interestedContactables;
    }

    public void setOwner(Contactable owner) {
        this.owner = owner;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public void setResponsibleContactables(Collection<Contactable> responsibleContactables) {
        this.responsibleContactables = responsibleContactables;
    }

    public void setCachedStartDate(Date cachedStartDate) {
        this.cachedStartDate = cachedStartDate;
    }

    public void setCachedStopDate(Date cachedStopDate) {
        this.cachedStopDate = cachedStopDate;
    }

    public String getProjectPathName() {
        List<String> nodeNames = new ArrayList<String>();
        nodeNames.add(getName());
        Node pNode = getParentNode();
        while(pNode != null) {
            nodeNames.add(0, pNode.getName());
            if (pNode instanceof Project)
                break;
            if (pNode instanceof SystemNode)
                break;
            pNode = pNode.getParentNode();
        }
        return StringUtils.join(nodeNames, '/');
    }

    public void clearInterestedContactables() {
        for (Contactable contactable: interestedContactables) {
            contactable.getInterestedTasks().remove(this);
        }
        interestedContactables.clear();
    }

    public void addInterestedContactable(Contactable contactable) {
        if (!interestedContactables.contains(contactable)) {
            interestedContactables.add(contactable);
            contactable.getInterestedTasks().add(this);
        }
    }

    public void clearResponsibleContactables() {
        for (Contactable contactable: responsibleContactables) {
            contactable.getResponsibleTasks().remove(this);
        }
        responsibleContactables.clear();
    }

    public void addResponsibleContactable(Contactable contactable) {
        if (!responsibleContactables.contains(contactable)) {
            responsibleContactables.add(contactable);
            contactable.getResponsibleTasks().add(this);
        }
    }

}
